package com.beiding.jsonanalyzer.flow.branchevents;

import java.util.logging.Logger;

/**
 * 立刻结束
 *
 * @author 丁常磊
 * @date 2022/5/26 13:12
 */

public class StopNow {

    private static Logger logger = Logger.getLogger(StopNow.class.getName());

    /**
     * 终止状态
     */
    private StopStatus status;


    /**
     * 提示信息
     */
    private String msg;


    /**
     * 异常
     */
    private Exception exception;


    private StopNow() {

    }

    public static StopNow success() {
        return success("执行成功");
    }

    public static StopNow success(String msg) {
        StopNow stopNow = new StopNow();
        stopNow.status = StopStatus.SUCCESS;
        stopNow.msg = msg;
        return stopNow;
    }

    public static StopNow error(Exception exception) {
        return error(exception, "执行异常");
    }

    public static StopNow error(Exception exception, String msg) {
        StopNow stopNow = new StopNow();
        stopNow.status = StopStatus.ERROR;
        stopNow.exception = exception;
        stopNow.msg = msg;
        return stopNow;
    }


    public Exception getException() {
        return exception;
    }

    public StopStatus getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }


    /**
     * 打印执行结果
     */
    public void log() {
        if (status == StopStatus.SUCCESS) {
            logger.info("执行结束: 成功");
            if (msg != null) {
                logger.info(msg);
            }
        } else if (status == StopStatus.ERROR) {
            logger.warning("执行结束: 失败");
            if (msg != null) {
                logger.warning(msg);
            }
            if (exception != null) {
                exception.printStackTrace();
            }
        }
    }

    /**
     * 终止状态
     */
    public enum StopStatus {

        /**
         * 成功
         */
        SUCCESS,
        /**
         * 异常
         */
        ERROR;

    }


}
